package kr.hkit.android_caloriecheck.adapter;

import java.util.ArrayList;
import java.util.Calendar;

import kr.hkit.android_caloriecheck.R;
import kr.hkit.android_caloriecheck.dao.DAO;
import kr.hkit.android_caloriecheck.dto.MyItem;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MyListAdapter extends BaseAdapter {
	Context maincon;
	LayoutInflater inflater;
	ArrayList<MyItem> arList;
	int layout;

	String eatName;
	int eatKcal;
	String exerciseName;
	int exerciseKcal;

	DAO dao;

	Calendar calendar;
	int year;
	int month;
	int day;

	String str;
	int mSelect;

	public MyListAdapter(Context context, ArrayList<MyItem> arList, int layout,
			String str) {
		this.maincon = context;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.arList = arList;
		this.layout = layout;
		this.str = str;

		calendar = Calendar.getInstance();
		year = calendar.get(Calendar.YEAR);
		month = calendar.get(Calendar.MONTH);
		day = calendar.get(Calendar.DATE);

		dao = DAO.open(maincon);
	}

	public int getCount() {
		return arList.size();
	}

	public Object getItem(int position) {
		return arList.get(position).getName();
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;

		if (convertView == null) {
			convertView = inflater.inflate(layout, parent, false);
		}

		TextView name = (TextView) convertView.findViewById(R.id.name);
		name.setText(arList.get(position).getName());

		TextView kcal = (TextView) convertView.findViewById(R.id.kcal);
		kcal.setText(String.valueOf(arList.get(position).getKcal()));

		Button add = (Button) convertView.findViewById(R.id.add);
		add.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (str.equals("food")) {
					eatName = arList.get(pos).getName();
					eatKcal = arList.get(pos).getKcal();
					
					new DatePickerDialog(maincon, mDateSetListener, year, month, day).show();
							
				} else {
					new AlertDialog.Builder(maincon)
							.setTitle("운동 시간을 선택하세요.")
							.setSingleChoiceItems(R.array.time, 0,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											mSelect = which;
										}
									})
							.setPositiveButton("확인",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											switch (mSelect) {
											case 0:
												exerciseKcal = arList.get(pos)
														.getKcal();
												break;
											case 1:
												exerciseKcal = arList.get(pos)
														.getKcal() * 2;
												break;
											case 2:
												exerciseKcal = arList.get(pos)
														.getKcal() * 3;
												break;
											case 3:
												exerciseKcal = arList.get(pos)
														.getKcal() * 4;
												break;
											case 4:
												exerciseKcal = arList.get(pos)
														.getKcal() * 5;
												break;
											case 5:
												exerciseKcal = arList.get(pos)
														.getKcal() * 6;
												break;
											}
											exerciseName = arList.get(pos).getName();
											
											new DatePickerDialog(maincon, mDateSetListener, year, month, day).show();
										}
									}).setNegativeButton("취소", null).show();
				}
			}
		});

		return convertView;
	}

	DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener() {
		public void onDateSet(DatePicker view, int y, int m, int d) {
			if (str.equals("food")) {
				dao.foodListInsert(eatName, eatKcal, y, m + 1, d);
				Toast.makeText(maincon, eatName + " 추가", Toast.LENGTH_SHORT)
						.show();
			} else {
				dao.exerciseListInsert(exerciseName, exerciseKcal, y, m + 1, d);
				Toast.makeText(maincon, exerciseName + " 추가",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

}
